package cn.mugutu.finance.service.impl;

import cn.mugutu.finance.domain.BaiJiaContent;
import cn.mugutu.finance.service.BaiJiaContentService;
import cn.mugutu.finance.service.system.impl.BaseServiceImpl;
import cn.mugutu.finance.utils.JsoupUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gan on 2019/2/13 22:34.
 */

@Service
@Transactional
public class BaiJiaContentServiceImpl extends BaseServiceImpl<BaiJiaContent> implements BaiJiaContentService {

    @Override
    public Map<String, String> fetchList() throws Exception {
        return parse1();
    }


    /**
     * 针对不同的url文章列表要写不同的解析器
     */

    private static Map<String, String> parse1() throws Exception {
        Map<String, String> map = new HashMap<>();

        int page = 1; //页数
        while (true) {
            Thread.sleep(1000);
            try {
                Connection con = Jsoup.connect("http://www.zimeika.com/article/lists/toutiao.html?cate_id=&time_type=&read_order=&type=&author=1&author_id=835002&author_name=全球艺术精选&title=&p=" + page);
                Document doc = con.get();
                Element tbody = doc.getElementsByTag("tbody").get(0);
                Elements contentDivs = tbody.getElementsByTag("tr");
                if (contentDivs != null && contentDivs.size() != 0) {
                    for (int i = 0; i < contentDivs.size(); i++) {


                        String href = contentDivs.get(i).getElementsByTag("a").get(0).attr("href");
                        contentDivs.get(i).getElementsByTag("a").get(0).attr("href", "http://www.zimeika.com" + href);
                        String html = contentDivs.get(i).html();
                        map.put(html, "http://www.zimeika.com" + href);

                        System.out.println("page==>" + page + "  content=====>" + html);
                        System.out.println("==================>" + href);

                    }
                } else {
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }

            ++page;
        }

        return map;
    }


    public static void main(String[] args) throws Exception {
        parse1();
        System.out.println("运行完毕。。。。");
    }

}
